﻿using System.Reflection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace zhwy.saas.api.Extensions.Services
{
    public static class ServiceExtensions
    {
        public static IServiceCollection AddServices(this IServiceCollection services)
        {
            var serviceTypes = Assembly.GetExecutingAssembly().GetTypes()
                .Where(t => t.IsClass && !t.IsAbstract && t.Name.EndsWith("Service"));

            foreach (var type in serviceTypes)
            {
                var interfaceType = type.GetInterfaces()
                    .FirstOrDefault(i => i.Name == $"I{type.Name}");

                if (interfaceType != null)
                {
                    services.TryAddScoped(interfaceType, type);
                }
            }

            return services;
        }
    }
}
